using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GamePool : MonoBehaviour {

    /// <summary>
    /// The singleton of GamePool
    /// </summary>
    private static GamePool instance;
    public static GamePool Instance
    {
        get
        {
            if (instance != null)
            {
                return instance;
            }
            else
            {
                Debug.LogError("Instance is error , Pool is no exist");
                return null;
            }
        }
    }

    /// <summary>
    /// The Object Prefabs
    /// </summary>
    public List<GameObject> objectPrefabs = new List<GameObject>();

    /// <summary>
    /// The Objects in this pool
    /// </summary>
    public List<List<GameObject>> objectsInPool;

    void Awake()
    {
        instance = this;
    }

    void Start()
    {
        int count = objectPrefabs.Count;
        objectsInPool = new List<List<GameObject>>();
        for (int i = 0; i < count; i++)
        {
            List<GameObject> Objects = new List<GameObject>();
            objectsInPool.Add(Objects);
            GameItem item = objectPrefabs[i].GetComponent<GameItem>();
            if (item == null)
            {
                item = objectPrefabs[i].AddComponent<GameItem>();
            }
            item.SetIndex(i);
        }
    }

    /// <summary>
    /// Get a new Object from pool , if it is not in the pool
    /// then will be created new object in this pool
    /// </summary>
    /// <param name="original">the original gameobject</param>
    /// <returns></returns>
    public GameObject GetObject(GameObject original)
    {
        GameObject clone = null;
        int count = objectPrefabs.Count;
        for (int i = 0; i < count; i++)
        {
            GameObject prefab = objectPrefabs[i];
            if (prefab.name == original.name)
            {
                if (objectsInPool[i].Count > 0)
                {
                    clone = objectsInPool[i][0];
                    objectsInPool[i].RemoveAt(0);
                    clone.SetActive(true);
                }
                else
                {
                    clone = Instantiate(original) as GameObject;
                    clone.name = original.name;
                }
                return clone;
            }
        }
        if (clone == null)
        {
            objectPrefabs.Add(original);
            objectsInPool.Add(new List<GameObject>());
            clone = Instantiate(original) as GameObject;
            clone.name = original.name;
        }
        return clone;
    }

    public GameItem GetGameItem(GameItem original)
    {
        GameItem clone = null;
        if (original.poolIndex != -1)
        {
            GameObject prefab = objectPrefabs[original.poolIndex];
            if (prefab.name == original.name)
            {
                if (objectsInPool[original.poolIndex].Count > 0)
                {
                    clone = objectsInPool[original.poolIndex][0].GetComponent<GameItem>();
                    objectsInPool[original.poolIndex].RemoveAt(0);
                    clone.gameObject.SetActive(true);
                }
                else
                {
                    clone = Instantiate(original) as GameItem;
                    clone.name = original.name;
                }
                return clone;
            }
        }
        return clone;
    }

    /// <summary>
    /// return object to pool
    /// </summary>
    /// <param name="obj">object to be pooled</param>
    public void DestroyObject(GameObject obj)
    {
        int count = objectPrefabs.Count;
        for (int i = 0; i < count; i++)
        {
            if (obj.name == objectPrefabs[i].name)
            {
                obj.SetActive(false);
                objectsInPool[i].Add(obj);
                return;
            }
        }
        Destroy(obj);
        Debug.LogError("Obj is no exist in this pool");
    }
}